package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmy.cultivate.entity.Subjects;
import com.xmy.cultivate.entity.Transcript;
import com.xmy.cultivate.entity.TranscriptSet;
import com.xmy.cultivate.service.ISubjectsService;
import com.xmy.cultivate.service.ITranscriptSetService;
import com.xmy.cultivate.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2023-04-14
 */
@RestController
@RequestMapping("/api/transcriptSet")
public class TranscriptSetController extends BaseController{
    @Autowired
    ITranscriptSetService iTranscriptSetService;

    @Autowired
    IdWorker idWorker;

    @GetMapping()
    public Result findAll(@RequestParam Map reMap){
        try{
            Integer pageNum = StringUtils.isEmpty((String)reMap.get("pageNum")) ? 1 : Integer.valueOf((String)reMap.get("pageNum"));
            Integer pageSize = StringUtils.isEmpty((String)reMap.get("pageSize")) ? 10: Integer.valueOf((String)reMap.get("pageSize"));
            IPage<TranscriptSet> iPage = new Page<>(pageNum,pageSize);
            iTranscriptSetService.page(iPage);
            return new Result(Code.OK,new PageResult(iPage.getRecords(),iPage.getTotal(),iPage.getPages()));
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id){
        try{
            TranscriptSet transcriptSet = iTranscriptSetService.getById(id);
            return new Result(Code.OK,transcriptSet);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }


    @PostMapping()
    public Result save(@RequestBody TranscriptSet transcriptSet){
        try{
            boolean rs = iTranscriptSetService.save(transcriptSet);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @PutMapping()
    public Result update(@RequestBody TranscriptSet transcriptSet){
        try{
            transcriptSet.setPatriarchExamName(CommonUtil.getExamNameForId(transcriptSet.getPatriarchExamNum()));
            transcriptSet.setTeacherExamName(CommonUtil.getExamNameForId(transcriptSet.getTeacherExamNum()));
            boolean rs = iTranscriptSetService.updateById(transcriptSet);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id){
        try{
            boolean rs =  iTranscriptSetService.removeById(id);
            return new Result(rs?Code.OK:Code.ERROR);
        }catch (Exception e){
            return new Result(Code.ERROR,e.getMessage());
        }
    }
}
